package com.sinosoft.dsp.persistence.businessconcept.atom;

import javax.measure.unit.Unit;

import com.sinosoft.dsp.persistence.businessconcept.utils.UnitHelper;

/**
 * 数量原子类型的Bean
 * 
 * @author 潘巍（Peter Pan）
 * @since 2010-11-9 下午05:39:11
 */
public class SinoQuantityBean<T> implements ISinoAtomType {

	/**
	 * 数字
	 */
	private T num;

	/**
	 * 度量单位
	 */
	private Unit<?> unit;

	public SinoQuantityBean() {
	}

	public SinoQuantityBean(T num, String unitName) {
		this.num = num;
		this.unit = UnitHelper.getUnit(unitName);
	}

	public SinoQuantityBean(T num, Unit<?> unit) {
		this.num = num;
		this.unit = unit;
	}

	public T getNum() {
		return num;
	}

	public SinoQuantityBean<T> setNum(T num) {
		this.num = num;
		return this;
	}

	public Unit<?> getUnit() {
		return unit;
	}

	public String getUnitName() {
		return UnitHelper.getUnitName(unit);
	}

	public SinoQuantityBean<T> setUnit(String unitName) {
		this.unit = UnitHelper.getUnit(unitName);
		return this;
	}

	public SinoQuantityBean<T> setUnit(Unit<?> unit) {
		this.unit = unit;
		return this;
	}

	@SuppressWarnings("unchecked")
	public void setLongNum(Long n) {
		if (n != null) {
			if (num instanceof Long) {
				this.num = (T) n;
			} else if (num instanceof Double) {
				this.num = (T) (Double) n.doubleValue();
			} else if (num instanceof java.math.BigDecimal) {
				this.num = (T) java.math.BigDecimal.valueOf(n);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void setDoubleNum(Double n) {
		if (n != null) {
			if (num instanceof Long) {
				this.num = (T) (Long) n.longValue();
			} else if (num instanceof Double) {
				this.num = (T) n;
			} else if (num instanceof java.math.BigDecimal) {
				this.num = (T) java.math.BigDecimal.valueOf(n);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void setBigDecimalNum(java.math.BigDecimal n) {
		if (n != null) {
			if (num instanceof Long) {
				this.num = (T) (Long) n.longValue();
			} else if (num instanceof Double) {
				this.num = (T) (Double) n.doubleValue();
			} else if (num instanceof java.math.BigDecimal) {
				this.num = (T) n;
			}
		}
	}

	public Long getLongNum() {
		if (num != null) {
			if (num instanceof Long) {
				return (Long) num;
			} else if (num instanceof Double) {
				return ((Double) num).longValue();
			} else if (num instanceof java.math.BigDecimal) {
				return ((java.math.BigDecimal) num).longValue();
			}
		}
		return null;
	}

	public Double getDoubleNum() {
		if (num != null) {
			if (num instanceof Long) {
				return ((Long) num).doubleValue();
			} else if (num instanceof Double) {
				return (Double) num;
			} else if (num instanceof java.math.BigDecimal) {
				return ((java.math.BigDecimal) num).doubleValue();
			}
		}
		return null;
	}

	public java.math.BigDecimal getBigDecimalNum() {
		if (num != null) {
			if (num instanceof Long) {
				return java.math.BigDecimal.valueOf(((Long) num));
			} else if (num instanceof Double) {
				return java.math.BigDecimal.valueOf(((Double) num));
			} else if (num instanceof java.math.BigDecimal) {
				return (java.math.BigDecimal) num;
			}
		}
		return null;
	}

	@Override
	public boolean isEmpty() {
		return this.num == null;
	}

}
